﻿using Quartz;
using SqlSugar;

namespace DongFangGuoKai.WMSPlatform.WebApi.Jobs.JobDetails
{
    /// <summary>
    /// 定时清理日志文件
    /// </summary>
    [DisallowConcurrentExecution]
    public class DataCleanupJob : IJob
    {

        private readonly ISqlSugarClient _sugarClient;
        private readonly ILogger<DataCleanupJob> _logger;


        public DataCleanupJob(ISqlSugarClient sugarClient, ILogger<DataCleanupJob> logger)
        {
            _sugarClient = sugarClient;
            _logger = logger;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                ExecApiLog();

            }
            catch (Exception ex)
            {
                _logger.LogError("ExecApilog:" + ex.Message);
            }


            try
            {
                ExecDirectoryLog();
            }
            catch (Exception ex)
            {
                _logger.LogError("ExceDirectorylog：" + ex.Message);
            }



            await Task.CompletedTask;
        }

        private void ExecApiLog()
        {

            _logger.LogInformation("Execting TRUNCATE TABLE WMSDBA.t_QueceRecard;");
            _sugarClient.Ado.ExecuteCommand("TRUNCATE TABLE WMSDBA.t_QueceRecard;");

            _logger.LogInformation("Execting TRUNCATE TABLE WMSDBA.t_ApiLog;");
            _sugarClient.Ado.ExecuteCommand("TRUNCATE TABLE WMSDBA.t_ApiLog;");
        }

        private void ExecDirectoryLog()
        {
            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            string logDirectoryName = "Logs";
            List<string> logLevelNames = new List<string>() { "Debug", "Error", "Fatal", "Infomation", "Warning" };
            string runTimesPath;
            bool isProduction = environment == "Production";
            if (isProduction)
            {
                runTimesPath = AppDomain.CurrentDomain.BaseDirectory;

            }
            else
            {
                runTimesPath = Environment.CurrentDirectory;
            }
            runTimesPath = Path.Combine(runTimesPath, logDirectoryName);
            if (Directory.Exists(runTimesPath))
            {
                //是否删除目标文件夹中的子文件夹及子文件
                Directory.Delete(runTimesPath, true);
                //logLevelNames.ForEach(x =>
                //{

                //    if(Directory.Exists())

                //});
            }
        }

    }
}
